package com.xm.xmapp.tool;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.Toast;

import com.xm.xmapp.app.MyAppcation;

/**
 * Created by LDW on 2017/7/14.
 */
public class AndroidTool {

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static int getVersionCode() {
        try {
            PackageManager manager = MyAppcation.getMyAppcation()
                    .getPackageManager();
            PackageInfo info = manager.getPackageInfo(MyAppcation
                    .getMyAppcation().getPackageName(), 0);
            int version = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 获取手机型号
     *
     */
    public static String getMODEL() {
        return android.os.Build.MODEL;
    }

    public static void showMessage(Object text){
        if(text!=null)
        Toast.makeText(MyAppcation.getMyAppcation(),text.toString(),Toast.LENGTH_SHORT).show();
    }

    public static int dp2px(float dpValue) {
        final float scale = MyAppcation.getMyAppcation().getResources()
                .getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }
}
